package com.fyz.aaronfjava.common.config;

import com.fyz.aaronfjava.common.handler.ApiRoleHandler;
import com.fyz.aaronfjava.common.handler.AuthHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author：AaronF
 * @Date：2024/1/31 15:15
 * @description 自定义web配置
 */
@Configuration
public class CustomWebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ApiRoleHandler apiRoleHandler;
    @Autowired
    private AuthHandler authHandler;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns匹配拦截路径 excludePathPatterns排除拦截路径 一般会把静态资源排除掉
        // 权限验证拦截器
        registry.addInterceptor(apiRoleHandler).addPathPatterns("/**").excludePathPatterns("/test/**");
        // 身份验证拦截器
        registry.addInterceptor(authHandler).addPathPatterns("/**").excludePathPatterns("/test/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //所有接口
        registry.addMapping("/**")
                //是否发送 Cookie
                .allowCredentials(true)
                //支持域
                .allowedOriginPatterns("*")
                //支持方法
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .exposedHeaders("*");
    }
}
